package com.xuxin.nantai.utils;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;

import java.util.Calendar;

/**
 * Created by xu_xin
 * Created on 2020.06.16 0016
 * 选择时间
 */
public class TimeUtils {

    public interface onListener {
        void onShow(String time);
    }

    public static void showDate(Context context, onListener listener) {
        Calendar calendar = Calendar.getInstance();
        new DatePickerDialog(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, (view, year, month, dayOfMonth) -> {
            month++;
            String my_month = month >= 10 ? String.valueOf(month) : ("0" + month);
            String my_day = dayOfMonth >= 10 ? String.valueOf(dayOfMonth) : ("0" + dayOfMonth);
            String data = year + "-" + my_month + "-" + my_day;
            listener.onShow(data);
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    public static void showTime(Context context, onListener listener) {
        Calendar calendar = Calendar.getInstance();
        new DatePickerDialog(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, (view, year, month, dayOfMonth) -> {
            month++;
            String my_month = month >= 10 ? String.valueOf(month) : ("0" + month);
            String my_day = dayOfMonth >= 10 ? String.valueOf(dayOfMonth) : ("0" + dayOfMonth);
            String data = year + "-" + my_month + "-" + my_day;
            new TimePickerDialog(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,
                    (views, hourOfDay, minute) -> {
                        String my_hour = hourOfDay >= 10 ? String.valueOf(hourOfDay) : ("0" + hourOfDay);
                        String my_minute = minute >= 10 ? String.valueOf(minute) : ("0" + minute);
                        String time = data + " " + my_hour + ":" + my_minute + ":00";
                        listener.onShow(time);
                    }
                    , calendar.get(Calendar.HOUR_OF_DAY)
                    , calendar.get(Calendar.MINUTE)
                    , true).show();
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
    }
}